var express = require('express');
var md5 = require('./../tools/md5.js')
var AES =require('./../tools/AES.js')
var router = express.Router();

var db = require('../sqldb/index.js');
var sysUser =require('../model/sysUser.js')

/* GET users listing. */
router.post('/', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return sysUser(db.sequelize).findOne({
			where:{
				studentNumber:req.body.studentNumber
			}
		},{
			transaction:t
		}).then(function(data){
			if (md5(req.body.password)==md5(data.password)) {
				res.cookie('hasLogin', md5(1), { 
					expires: new Date(Date.now() + 10000 * 60 * 60 * 24 * 0.25)
				});
				res.cookie('studentNumber',AES.aesKey(data.studentNumber), { 
					expires: new Date(Date.now() + 10000 * 60 * 60 * 24 * 0.25)
				})
				res.json({
					status:1,
					msg:'',
					result:data,
					hasLogin:1,
					identity:2
				})
			}else{
				res.json({
					status:0,
					msg:'',
					result:'',
					hasLogin:0,
					identity:0
				})
			}
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
});

router.post('/out', function(req, res, next) {
	res.cookie('hasLogin', md5(0), { 
		expires: new Date(Date.now() - 10000 * 60 * 60 * 24 * 7)
	});
	res.cookie('studentNumber','', { 
		expires: new Date(Date.now() - 10000 * 60 * 60 * 24 * 0.25)
	});
	res.json({
		status:1,
		msg:'',
		result:'',
		hasLogin:0,
		identity:0
	})
});

module.exports = router;
